package co.yixiang.yshop.module.product.service.category;

import co.yixiang.yshop.framework.common.enums.CommonStatusEnum;
import co.yixiang.yshop.framework.common.exception.ErrorCode;
import co.yixiang.yshop.module.product.controller.admin.category.vo.ProductCategoryCreateReqVO;
import co.yixiang.yshop.module.product.controller.admin.category.vo.ProductCategoryListReqVO;
import co.yixiang.yshop.module.product.controller.admin.category.vo.ProductCategoryTreeRespVO;
import co.yixiang.yshop.module.product.controller.admin.category.vo.ProductCategoryUpdateReqVO;
import co.yixiang.yshop.module.product.convert.category.ProductCategoryConvert;
import co.yixiang.yshop.module.product.dal.dataobject.category.ProductCategoryDO;
import co.yixiang.yshop.module.product.dal.mysql.category.ProductCategoryMapper;
import co.yixiang.yshop.module.product.service.storeproductcateref.StoreProductCategoryRefService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.module.product.enums.ErrorCodeConstants.*;

/**
 * 商品分类 Service 实现类
 *
 * @author yshop
 */
@Service
@Validated
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategoryDO> implements ProductCategoryService {

    @Resource
    private ProductCategoryMapper productCategoryMapper;
    @Resource
    private StoreProductCategoryRefService productCategoryRefService;

    @Override
    public Long createCategory(ProductCategoryCreateReqVO createReqVO) {
        // 校验父分类存在
        validateParentProductCategory(createReqVO.getParentId());

        // 插入
        ProductCategoryDO category = ProductCategoryConvert.INSTANCE.convert(createReqVO);
        productCategoryMapper.insert(category);
        // 返回
        return category.getId();
    }

    @Override
    public void updateCategory(ProductCategoryUpdateReqVO updateReqVO) {
        // 校验分类是否存在
        validateProductCategoryExists(updateReqVO.getId());
        // 校验父分类存在
        validateParentProductCategory(updateReqVO.getParentId());
        if(updateReqVO.getParentId().toString().equals(updateReqVO.getId().toString())){
            throw exception(new ErrorCode(202501200,"上级分类不能选择自己"));
        }
        // 更新
        ProductCategoryDO updateObj = ProductCategoryConvert.INSTANCE.convert(updateReqVO);
        productCategoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteCategory(Long id) {
        // 校验分类是否存在
        validateProductCategoryExists(id);
        // 校验是否还有子分类
        if (productCategoryMapper.selectCountByParentId(id) > 0) {
            throw exception(CATEGORY_EXISTS_CHILDREN);
        }
        // TODO yshop 补充只有不存在商品才可以删除
        // 删除
        productCategoryMapper.deleteById(id);
    }

    private void validateParentProductCategory(Long id) {
        // 如果是根分类，无需验证
        if (Objects.equals(id, ProductCategoryDO.PARENT_ID_NULL)) {
            return;
        }
        // 父分类不存在
        ProductCategoryDO category = productCategoryMapper.selectById(id);
        if (category == null) {
            throw exception(CATEGORY_PARENT_NOT_EXISTS);
        }

        ProductCategoryDO storeCategory = productCategoryMapper.selectById(id);

        // 最多二级
        if (!Objects.equals(storeCategory.getParentId(), ProductCategoryDO.PARENT_ID_NULL)) {
            throw exception(CATEGORY_PARENT_NOT_FIRST_LEVEL);
        }

    }

    private void validateProductCategoryExists(Long id) {
        ProductCategoryDO category = productCategoryMapper.selectById(id);
        if (category == null) {
            throw exception(CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public ProductCategoryDO getCategory(Long id) {
        return productCategoryMapper.selectById(id);
    }

    @Override
    public void validateCategory(Long id) {
        ProductCategoryDO category = productCategoryMapper.selectById(id);
        if (category == null) {
            throw exception(CATEGORY_NOT_EXISTS);
        }
        if (Objects.equals(category.getStatus(), CommonStatusEnum.DISABLE.getStatus())) {
            throw exception(CATEGORY_DISABLED, category.getName());
        }
    }

    @Override
    public Integer getCategoryLevel(Long id) {
        if (Objects.equals(id, ProductCategoryDO.PARENT_ID_NULL)) {
            return 0;
        }
        int level = 1;
        for (int i = 0; i < 100; i++) {
            ProductCategoryDO category = productCategoryMapper.selectById(id);
            // 如果没有父节点，break 结束
            if (category == null
                    || Objects.equals(category.getParentId(), ProductCategoryDO.PARENT_ID_NULL)) {
                break;
            }
            // 继续递归父节点
            level++;
            id = category.getParentId();
        }
        return level;
    }

    @Override
    public List<ProductCategoryDO> getEnableCategoryList(ProductCategoryListReqVO listReqVO) {
        return productCategoryMapper.selectList(listReqVO);
    }

    @Override
    public List<ProductCategoryDO> getEnableCategoryList() {
        return productCategoryMapper.selectListByStatus(CommonStatusEnum.ENABLE.getStatus());
    }

    @Override
    public List<ProductCategoryTreeRespVO> getCategoryTree() {
        List<ProductCategoryDO> productCategoryDOS =
                productCategoryMapper.selectListByStatus(CommonStatusEnum.ENABLE.getStatus());
        List<ProductCategoryTreeRespVO> list = ProductCategoryConvert.INSTANCE.convertTreeList(productCategoryDOS);
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        Map<Long, List<ProductCategoryTreeRespVO>> categoryMap =
                list.stream().collect(Collectors.groupingBy(ProductCategoryTreeRespVO::getParentId));
        // 获取最上级分类
        List<ProductCategoryTreeRespVO> roots = categoryMap.get(0l);
        if (CollectionUtils.isEmpty(roots)) {
            return new ArrayList<>();
        }
        getTree(roots, categoryMap);
        return roots;
    }

    private void getTree(List<ProductCategoryTreeRespVO> roots, Map<Long, List<ProductCategoryTreeRespVO>> categoryMap) {
        for (ProductCategoryTreeRespVO root : roots) {
            List<ProductCategoryTreeRespVO> categoryList = categoryMap.get(root.getId());
            if (CollectionUtils.isEmpty(categoryList)) {
                continue;
            }
            getTree(categoryList, categoryMap);
            root.setChildren(categoryList);
        }
    }


    public List<Long> getCateIdListByCateId(Long cateId){
        List<Long> catIds = new ArrayList<>();
        if (cateId != null) {
            ProductCategoryDO productCategoryDO = this.getCategory(cateId);
            if (productCategoryDO != null) {
                if (Objects.equals(productCategoryDO.getParentId(), ProductCategoryDO.PARENT_ID_NULL)) {
                    catIds = this.list((Wrappers.<ProductCategoryDO>lambdaQuery()
                                    .eq(ProductCategoryDO::getParentId, productCategoryDO.getId())))
                            .stream().map(ProductCategoryDO::getId).collect(Collectors.toList());
                } else {
                    catIds.add(cateId);
                }
            }
        }
        return catIds;
    }

    @Override
    public List<Long> getProductIdListByCateId(Long cateId) {
        List<Long> cateIdList = this.getCateIdListByCateId(cateId);
        List<Long> productIdList = productCategoryRefService.getProductIdListByCateId(cateIdList);
        return productIdList;
    }

    @Override
    public Long getCateIdByName(String cateName) {
        ProductCategoryDO productCategoryDO = baseMapper.selectOne(ProductCategoryDO::getName, cateName);
        return null != productCategoryDO ? productCategoryDO.getId() : null;
    }

    ;

}
